package com.metronoteapp.adapter;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.metronoteapp.R;
import com.metronoteapp.model.GroupModel;

public class AllGroupAdapter extends BaseAdapter implements OnItemClickListener {
	private String TAG = AllGroupAdapter.class.getSimpleName();
	private LayoutInflater mInflater;
	private List<GroupModel> mData;
	private Context mContext;

	public AllGroupAdapter(Context context, List<GroupModel> _data) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		mData = _data;
		Log.v(TAG, "ListItemLoad: " + mData.size());
	}

	public class ViewHolder {
		TextView txtTitleGroup;
		TextView txtGroupId;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		GroupModel temp = mData.get(position);
		final ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = mInflater.inflate(R.layout.layout_metro_group_row, null);
			holder.txtTitleGroup = (TextView) view
					.findViewById(R.id.txtTitleGroup);
			holder.txtGroupId = (TextView) view
					.findViewById(R.id.txtGroupNoteId);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.txtTitleGroup.setText(temp.getGroupName().toString());
		holder.txtGroupId.setText(String.valueOf(temp.getGroupId()));
		String strTempColor = temp.getColor().toString();
		if(strTempColor.equals("Color1")){
			holder.txtTitleGroup.setBackgroundResource(R.color.Color1);
		}else if(strTempColor.equals("Color2")){
			holder.txtTitleGroup.setBackgroundResource(R.color.Color2);
		}else if(strTempColor.equals("Color3")){
			holder.txtTitleGroup.setBackgroundResource(R.color.Color3);
		}else if(strTempColor.equals("Color4")){
			holder.txtTitleGroup.setBackgroundResource(R.color.Color4);
		}else{
			holder.txtTitleGroup.setBackgroundResource(R.color.Color5);
		}
		return view;
	}

	@Override
	public int getCount() {
		try {
			return mData.size();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	// Call edit infor screen
	public void callEditScreen(int _position) {
		try {

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	// Return holder selected by button check
	public ViewHolder getViewHolder(View v) {
		if (v.getTag() == null) {
			return getViewHolder((View) v.getParent());
		}
		return (ViewHolder) v.getTag();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		// this.callEditScreen(arg2);
	}
}